package com.mohan.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mohan.bean.UserListInfo;
import com.mohan.dao.UserDao;
import com.mohan.pojo.User;
import com.mohan.service.UserService;
import com.mohan.tool.Token;
import com.mohan.tool.UserMap;
import com.mohan.util.BetweenUtil;
import com.mohan.util.PageData;
import com.mohan.util.TokenUserInfo;

@Service("userService")
public class UserSeverImpl implements UserService {
	@Resource
	private UserDao userDao;

	/**
	 * 插入一条用户信息
	 * 
	 * @param user
	 */
	@Override
	public void insertOneUser(User user) throws Exception {
		// TODO Auto-generated method stub
		this.userDao.insertUser(user);
	}

	/**
	 * 根据UserId和UserPwd获取用户信息
	 */
	@Override
	public User getUserByUserIdAndUserPwd(User user) {
		// TODO Auto-generated method stub

		return this.userDao.selectUserByUserIdAndUserPwd(user);
	}

	/**
	 * 处理用户登录
	 */
	@Override
	public int dealLogin(String userId, TokenUserInfo tokenUserInfo) {
		// TODO Auto-generated method stub
		/**
		 * 生成token
		 */
		String token = Token.createToken();
		System.out.println("token = " + token);

		/**
		 * 存入静态map为后面的请求做验证
		 */
		if (token != null) {
			tokenUserInfo.setToken(token);
			UserMap.UserId2TokenMap.put(userId, tokenUserInfo);

			return 0;
		} else {
			return -1;
		}
	}
	/**
	 * 查询userId是否引进存在于数据库中
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@Override
	public Integer userIdIsExist(String userId) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.selectUserIdIsExist(userId);
	}

	/**
	 * 根据role获取记录的总条数
	 * @return
	 * @throws Exception
	 */
	@Override
	public Integer getRecordSizeByRole(BetweenUtil role) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.selectRecordSizeByRole(role);
	}
	
	/**
	 * 分页查询用户列表
	 * @param uListInfo
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PageData> getRecordByRole(BetweenUtil role) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.selectRecordByPage(role);
	}

	/**
	 * 根据userId获取用户信息
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@Override
	public User getUserByUserId(String userId) throws Exception {
		// TODO Auto-generated method stub
		return this.userDao.selectUserByUserId(userId);
	}

	/**
	 * 根据userId更改用户信息
	 * @param user
	 * @throws Exception
	 */
	@Override
	public void updateUserInfoByUserId(User user) throws Exception {
		// TODO Auto-generated method stub
		this.userDao.updateUserByUserId(user);
	}
	
	/**
	 * 根据userId更改用户信息
	 * @param user
	 * @throws Exception
	 */
	@Override
	public void updateWxUserByUserId(User user) throws Exception{
		
	}
	
	/**
	 * 更改用户status
	 * @param user
	 */
	public void updateUserStatusByUserId(User user) throws Exception{
		this.userDao.updateUserStatusByUserId(user);
	}
	
	/**
	 * 根据userId删除用户信息
	 * @param userId
	 * @throws Exception
	 */
	@Override
	public void deleteUserByUserId(String userId) throws Exception {
		// TODO Auto-generated method stub
		this.userDao.deleteByUserId(userId);
	}
}
